<template>
  <div class="product-bar" ref="ProductBarRef"></div>
</template>

<script setup lang="ts">
import { getCategoryAndNumberAPI } from '@/api/overview'
import * as echarts from 'echarts'

import { onBeforeUnmount, onMounted, ref } from 'vue'
let handleResize = () => {}
const ProductBarRef = ref()
const ProductBar = async () => {
  data.value = await getCategoryAndNumberAPI()
  const keys = ref<string[]>([])
  const values = ref<number[]>([])
  data.value.data.forEach((item: { name: string; value: number }) => {
    keys.value.push(item.name)
    values.value.push(item.value)
  })

  const myUserPie = echarts.init(ProductBarRef.value)
  myUserPie.setOption({
    title: {
      text: '产品类别库存总价图',
      left: 'letf',
      textStyle: {
        fontSize: 15
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '8%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: keys.value,
        axisTick: {
          show: true,
          alignWithLabel: false
        }
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: '产品类别库存总价',
        type: 'bar',
        data: values.value,
        barWidth: '15%'
      }
    ]
  })
  const handleResize = () => {
    myUserPie.resize()
  }
  window.addEventListener('resize', handleResize)
}

const data = ref()

onMounted(() => {
  ProductBar()
})
onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped></style>
